home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / torus-sr.tar / torus-sr / torus / robot.c < prev    next >
C/C++ Source or Header  |  1991-06-19  |  4KB  |  157 lines

  1. # include "robots.h"
  2.  
  3. /*
  4.  * robot.c: most of the robot oriented stuff
  5.  */
  6.  
  7. struct robot {
  8.   bool  alive;  /* is this suker still kicking */
  9.   int  x;
  10.   int  y;
  11.   int  speed;  /* speed of robot 1 or 2 for now */
  12. } rbt_list[MAX_ROBOTS+1];
  13.  
  14. put_robots()  /* place some robots */
  15. {
  16.   register struct robot *r, *end;
  17.   register int x, y;
  18.  
  19.   robot_value += level*(5 + (max_robots/scrap_heaps));
  20.   max_robots += level*3+rnd(level*5);
  21.   if(max_robots > MAX_ROBOTS) max_robots = MAX_ROBOTS;
  22.   nrobots_alive = max_robots;
  23.   scrap_heaps = 0;  /* number of scrap heaps created */
  24.   end = &rbt_list[max_robots];
  25.   for(r = rbt_list; r < end; r++) {
  26.     for(;;) {
  27.       x = rndx();
  28.       y = rndy();
  29.       move(y,x);
  30.       if(inch() == ' ') break;
  31.     }
  32.     r->x = x;
  33.     r->y = y;
  34.     r->alive = TRUE;
  35.     r->speed = 1 +  ( rnd(10) > 6 );
  36.     if (r->speed == 2) addch(FROBOT);
  37.     else  addch(ROBOT);
  38.   }
  39. }
  40.  
  41. robots(speed)  /* Troops, Troooooops advance! */
  42. int speed;
  43. {
  44.   register struct robot *r, *end;
  45.   register int x_inc, y_inc, h_dist, v_dist;
  46.  
  47.   end = &rbt_list[max_robots];
  48.   for(r = rbt_list; r < end; r++) {
  49.     if(r->alive && r->speed >= speed ) {
  50.       mvaddch(r->y,r->x,' ');
  51.     }
  52.   }
  53.   for(r = rbt_list; r < end; r++) {
  54.     if (r->alive && r->speed >= speed )
  55.     {
  56.       x_inc = sign(my_x-r->x);
  57.       y_inc = sign(my_y-r->y);
  58.       /*
  59.       h_dist = (hrev) ? abs(my_x-WIDTH+r->x) : abs(my_x-r->x);
  60.       v_dist = (vrev) ? abs(my_y-HEIGHT+r->y) : abs(my_y-r->y);
  61.       if ((hsew || hrev) && (h_dist > WIDTH/2)) x_inc *= -1;
  62.       if ((vsew || vrev) && (v_dist > HEIGHT/2)) y_inc *= -1;
  63.       if ((hrev) && (v_dist > HEIGHT/2)) y_inc *= -1;
  64.       if ((vrev) && (h_dist > WIDTH/2)) x_inc *= -1;
  65.       */
  66.       if ( hsew && (abs(my_x-r->x) > (COLS-2)/2) )
  67.         x_inc *= -1;
  68.       if ( vsew && (abs(my_y-r->y) > (LINES-3)/2) )
  69.         y_inc *= -1;
  70.       r->y=vbound(r->y+y_inc,r->x+x_inc);
  71.       r->x=hbound(r->y+y_inc,r->x+x_inc);
  72.       move(r->y,r->x);
  73.       switch(inch()) {
  74.       case ME:  /* robot eat me */
  75.         addch(MUNCH);
  76.         dead = TRUE;
  77.         break;
  78.       case SCRAP:  /* robot walks into scrap heap */
  79.         r->alive = FALSE;
  80.         if(r->speed == 2)
  81.           score += (MULT * robot_value);
  82.         else  score += robot_value;
  83.         nrobots_alive--;
  84.         break;
  85.       case FROBOT:
  86.       case ROBOT:  /* two robots form scrap heap */
  87.         collision(r, end);
  88.         r->alive = FALSE;
  89.         addch(SCRAP);
  90.         if (r->speed == 2)
  91.           score += (MULT * robot_value);
  92.         else  score += robot_value;
  93.         nrobots_alive -= 2;
  94.         scrap_heaps++ ;
  95.         break;
  96.       case MUNCH:
  97.         break;
  98.       default:
  99.         if(r->speed == 2) addch(FROBOT);
  100.         else  addch(ROBOT);
  101.       }
  102.     }
  103.   }
  104. }
  105.  
  106. /* do two robots collide - if so - which two */
  107. collision(r, end)
  108. register struct robot *r, *end;
  109. {
  110.   register struct robot *find;
  111.  
  112.   for(find = rbt_list; find < end; find++) {
  113.     if(find->alive && r != find) {
  114.       if(r->x == find->x && r->y == find->y) {
  115.         find->alive = FALSE;
  116.         if (find->speed == 2)
  117.           score += (MULT * robot_value);
  118.         else score += robot_value;
  119.         return;
  120.       } /* end of if */
  121.     }
  122.   }
  123. }
  124.  
  125. screwdriver()  /* dismantle those robots ... */
  126. {
  127.   register int test_x, test_y;
  128.   register char *m ;
  129.   register struct robot *end,*find;
  130.   static char moves[] = "hjklyubn.";
  131.  
  132.   end = &rbt_list[max_robots];
  133.  
  134.   for(m = moves; *m; m++) {  /* let's see if there is a robot */
  135.     test_x = hbound(my_y+yinc(*m),my_x+xinc(*m));
  136.     test_y = vbound(my_y+yinc(*m),my_x+xinc(*m));
  137.     move(test_y,test_x);
  138.     switch(inch()) {
  139.     case FROBOT:
  140.     case ROBOT:  /* OK so there is now let's look for it */
  141.       for(find = rbt_list; find < end; find++) {
  142.         if(find->alive) {
  143.           if(test_x == find->x && test_y  == find->y) {
  144.             find->alive = FALSE;
  145.             if (find->speed == 2) score += (MULT * robot_value);
  146.             else score += robot_value;
  147.             } /* end of if */
  148.         } /* end of if */
  149.       } /* end of for */
  150.       addch(' ');
  151.       nrobots_alive--;
  152.       break;
  153.     } /* end of case */
  154.   }
  155.   free_teleports--;
  156. }
  157.